﻿using System;
using UnityEngine;

namespace USClientKernel.MVC
{
    public abstract class BaseController<T, E> : Behaviour 
        where T : BaseData<T>
        where E : ModelEvent<T>
    {
        private BaseModel<T, E> model;
        private BaseView<T, E> view;

        public BaseModel<T, E> Model { get => model; set => model = value; }
        public BaseView<T, E> View { get => view; set => view = value; }

        public virtual void LinkViewToModel()
        {
            if (null == model)
                throw new Exception("Link view to model error! model is null!");
            if (null == view)
                throw new Exception("Link view to model error! view is null!");
            view.Controller = this;
            model.RegisterModelEventHanler(view.OnModelEventHandler);
        }

        public virtual void UnlinkViewWithModel()
        {
            if (null == model)
                throw new Exception("Link view to model error! model is null!");
            if (null == view)
                throw new Exception("Link view to model error! view is null!");
            view.Controller = null;
            model.UnregisterModelEventHandler(view.OnModelEventHandler);
        }
    }
}
